//存放交互逻辑使用js代码,  javaScript模块化
//seckill.detail.init(params);
var seckill = {
    //封装秒杀相关ajax的url
    URL: {
        now: function () {
            return '/seckill/time/now';
        },
        exposer: function (seckillId) {
            return '/seckill/' + seckillId + '/exposer';
        },
        execution:function (seckillId,MD5) {
            return '/seckill/'+seckillId+'/'+MD5+'/execution';
        }
    },
    //验证手机号
    validatePhone: function (phone) {
        if (phone && phone.length == 11 && !isNaN(phone)) {
            return true;
        } else {
            return false;
        }
    },

    handleSeckillKill: function (seckillId, node) {
        //控制节点前都需要隐藏
        node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn"> 开始秒杀</button>');
        $.post(seckill.URL.exposer(seckillId), {}, function (result) {
                if (result && result['success']) {
                    var exposer = result['data'];
                    if (exposer['exposed']) {
                        //开启秒杀,//获取秒杀地址
                        var md5 = exposer['md5'];
                        console.log(md5);
                        var killUrl = seckill.URL.execution(seckillId,md5);
                        console.log(killUrl);
                        //one click绑定一次点击事件(防止重复点击操作)
                        $('#killBtn').one('click',function () {
                            //执行秒杀请求操作
                            //先禁用按钮
                            $(this).addClass('disabled');
                            //发送秒杀请求执行秒杀
                            $.post(killUrl,{},function (result) {
                                if(result && result['success']){
                                    var killResult = result['data'];
                                    var state = killResult['state'];
                                    var stateInfo = killResult['stateInfo'];
                                    //显示秒杀结果
                                    node.html('<span class="label label-success">'+stateInfo+' </span>')
                                }
                            })
                        });
                        node.show();
                    } else {
                        //未开启秒杀
                        var now = exposer['now'];
                        var start = exposer['start'];
                        var end = exposer['end'];
                        seckill.countdown(seckillId, now, start, end);
                    }
                }
            })
    },

    countdown: function (seckillId, nowTime, startTime, endTime) {
        var seckillBox = $('#seckill-box');
        //时间判断
        if (nowTime > endTime) {
            //秒杀结束
            seckillBox.html('秒杀结束！');
        } else if (nowTime < startTime) {
            var killTime = new Date(startTime + 1000);//防止用户计时时间偏移
            seckillBox.countdown(killTime, function (event) {
                var format = event.startTime('秒杀倒计时:%D天 %H时 %M分 %S秒');
                seckillBox.html(format);
            }).on('finish.countdown', function () { //完成倒计时, 获取秒杀地址，控制显示逻辑.执行秒杀
                seckill.handleSeckillKill(seckillId, seckillBox);
            });
        } else {
            //秒杀开始
            seckill.handleSeckillKill(seckillId,seckillBox);
        }
    },
    detail: {
        //详情页初始化
        init: function (params) {
            //手机验证和登录,计时交互,规划我们的交互流程
            var killPhone = $.cookie('killPhone');
            //在cookie中查找手机号
            var seckillId = params['seckillId'];
            if (!seckill.validatePhone(killPhone)) {//验证手机号
                //绑定Phone
                var killPhoneModal = $('#killPhoneModal');
                killPhoneModal.modal({
                    show: true, //显示弹出层
                    backdrop: 'static',//禁止位置关闭
                    keyboard: false//关闭键盘事件
                });
                $('#killPhoneBtn').click(function () {
                    var inputPhone = $('#killPhoneKey').val();
                    console.log(inputPhone + "==");
                    if (seckill.validatePhone(inputPhone)) {
                        //电话写入cookie
                        $.cookie('killPhone', inputPhone, {expires: 7, path: '/seckill'})
                        //刷新页面
                        window.location.reload();
                    } else {
                        $('#killPhoneMessage').hide().html('<label class="label-danger">手机号错误!</label>').show(300);
                    }
                });
            }
            //已经登录
            //计时交互
            var killPhone = $.cookie('killPhone');
            var startTime = params['startTime'];
            var endTime = params['endTime'];
          //  console.log(seckillId+"="+killPhone+"="+startTime+"="+endTime);
            $.get(seckill.URL.now(), {}, function (result) {
                if (result && result['success']) {
                    var nowTime = result['data'];
                    seckill.countdown(seckillId, nowTime, startTime, endTime);
                }
            })
        }
    }
}